package com.qk.controller;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("添加部门：{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }

    @GetMapping
    public Result listDepts(String name,
                            @RequestParam(required = false) Integer status,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询部门，参数：name={},status={},page={},pageSize={}",name,status,page,pageSize);
        PageResult<Dept> p = deptService.listDepts(name, status, page, pageSize);
        return Result.success(p);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询部门，参数：id={}",id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {
        log.info("修改部门：{}",dept);
        deptService.updateDept(dept);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除部门，参数：id={}",id);
        deptService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result getAll() {
        log.info("查询所有部门");
        List<Dept> list = deptService.getAll();
        return Result.success(list);
    }
}
